package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import service.UserService;
import service.WatchedFilmService;

import entity.User;
import entity.Watched;

@WebServlet(urlPatterns = { "/rent" })
public class Rent extends HttpServlet {

	WatchedFilmService wservice = new WatchedFilmService();

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		if (session.getAttribute("user") != null) {
			String idString = request.getParameter("id");
			// int e parse edilebilirmi diye check edilebilirmi

			User user = (User) session.getAttribute("user");
			if (idString != null && idString != "") {
				int id = Integer.parseInt(idString);
				if(!wservice.isWatchedByUser(id, user)){
					wservice.addUserToWatchedFilm(user, id);
				}				
			}
		}
		response.sendRedirect("home");
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
